﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace EasySL.Controls
{
    public static class Window
    {
        private static Loading loading;
        private static Grid dialogGrid;

        static Window()
        {
            UserControl rootPage = Application.Current.RootVisual as UserControl;
            Panel panel = VisualTreeHelper.GetChild(rootPage, 0) as Panel;
            if (panel == null)
                return;

            if (loading == null)
            {
                loading = new Loading();
                loading.Visibility = Visibility.Collapsed;
                panel.Children.Add(loading);
            }

            if (dialogGrid == null)
            {
                dialogGrid = new Grid();
                panel.Children.Add(dialogGrid);
            }
        }

        public static void ShowLoading(string message)
        {
            if (loading != null)
            {
                loading.Dispatcher.BeginInvoke(() =>
                {
                    loading.Visibility = Visibility.Visible; 
                    loading.SetLoadingText(message);
                });
            }
        }

        public static void HideLoading()
        {
            if (loading != null)
                loading.Dispatcher.BeginInvoke(() => loading.Visibility = Visibility.Collapsed);
        }

        public static void Alert(string msg)
        {
            if (dialogGrid != null)
                dialogGrid.Dispatcher.BeginInvoke(() =>
                    {
                        dialogGrid.Children.Clear();
                        dialogGrid.Children.Add(new Alert(msg));
                    }
                    );
        }

        public static void Confirm(string msg, OnConfirm onConfirm)
        {
            if (dialogGrid != null)
                dialogGrid.Dispatcher.BeginInvoke(() =>
                {
                    dialogGrid.Children.Clear();
                    dialogGrid.Children.Add(new Confirm(msg, onConfirm));
                }
                );
        }

        public static void ShowOpPanel(string title, UserControl panel, OnSubmit onSubmit)
        {
            if (dialogGrid != null)
                dialogGrid.Dispatcher.BeginInvoke(() =>
                {
                    dialogGrid.Children.Clear();
                    dialogGrid.Children.Add(new OPPanel(title, panel, onSubmit));
                }
                );
        }

    }
}
